var express = require('express');
var router = express.Router();
const shsDB = require("./../../db/index.js");

router.post("/set", function(req, res) {
	let obj = {
		code: 201,
		message: "",
		data: {
			message: "", //业务消息
		}
	}
	try {
		let item = req.body
		let fromid = req.shsImUserInfo.fromId || ""
		let sql = `INSERT INTO \`imdb\`.\`hobby\`(\`id\`, \`hobbyname\`, \`fromID\`, \`status\`, \`hot\`, \`createTime\`, \`updateTime\`) 
		VALUES (null, '${item.hobbyname}', '${fromid}', 1, 0, '${getdate()}', '${getdate()}');`
		shsDB(sql).then(data => {
			obj.code = 200;
			obj.message = "添加成功";
			res.send(JSON.stringify(obj))
		}).catch(err => {
			obj.message = err.message;
			res.send(JSON.stringify(obj))
		})
	} catch (e) {}

})
router.post("/get", function(req, res) {
	let obj = {
		code: 201,
		message: "",
		data: {
			message: "", //业务消息
		}
	}
	try {
		let sql = ""
		if (req.body.hobbyname) {
			sql = `select * from hobby where hobbyname like '%${req.body.hobbyname}%'  ORDER BY hot desc limit 0,10`
		} else {
			let pageSizes = req.body.pageSizes || 10; //条数
			let pageNum = req.body.pageNum || 1; //第几页
			let pageStart = pageSizes * (pageNum - 1); //pageSizes*(pageNum-1)
			sql = `select * from hobby ORDER BY hot desc limit ${pageStart},${pageSizes}`
		}

		shsDB(sql).then(data => {
			obj.code = 200
			obj.data = data
			res.send(JSON.stringify(obj))
		}).catch(err => {
			obj.message = err.message;
			res.send(JSON.stringify(obj))
		})
	} catch (e) {}

})
module.exports = router;